package com.gitee.ricesugar.controller;

import cn.hutool.crypto.SecureUtil;
import com.gitee.ricesugar.common.Result;
import com.gitee.ricesugar.controller.request.LoginRequest;
import com.gitee.ricesugar.entity.User;
import com.gitee.ricesugar.service.impl.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    LoginService loginService;

    private static final String PASSWORD_SALT = "RiceSugar";

    @PostMapping()
    public Result login(@RequestBody LoginRequest request){
        request.setPassword(SecureUtil.md5(request.getPassword() + PASSWORD_SALT));
        return Result.success(loginService.login(request));
    }

    @PostMapping("/add")
    public Result add(@RequestBody User user){
        user.setPassword(SecureUtil.md5(user.getPassword() + PASSWORD_SALT));
        // 添加用户
        loginService.add(user);
        return Result.success();
    }
}
